package ssm;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by jack on 18/6/2.
 */
@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void saveBook(Book book) {
        getSession().save(book);
    }

    @Override
    public String findBookById(int id) {
        String hql = "select bookName from Book where id = ?";
        Query query = getSession().createQuery(hql).setInteger(0, id);
        String str = query.uniqueResult().toString();
        return str;
    }
}
